<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2025 feraner，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 feraner All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2020/6/7
// +----------------------------------------------------------------------

namespace app\command;

use app\common\facade\Arr;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 *
 * Class CreateVantArea
 * @package app\command
 * @author feraner<feraner@163.com>
 * @date 2020/6/7
 */
class CreateVantArea extends Command
{

    /**
     * 命令配置信息
     */
    protected function configure()
    {
        $this->setName('create:vant-area')->setDescription('生成vant 的地区数据');
    }


    protected function execute(Input $input, Output $output)
    {

        $this->__areaAddAdCode();

    }


    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    private function __areaAddAdCode()
    {

        //省份
        $provinceList = Db::name('area')
            ->where('area_deep', 1)
            ->where('area_parent_id', 0)
            ->order('area_sort', 'ASC')
            ->select() ?: [];

        $provinceStart = 11;

        //省份
        foreach ($provinceList as $item) {

            $provinceAdcode = $provinceStart . '0000';

            Db::name('area')
                ->where('area_id', Arr::get($item, 'area_id'))
                ->update(['area_code' => $provinceAdcode, 'area_deep' => 1]);

            $cityList = Db::name('area')
                ->where('area_parent_id', Arr::get($item, 'area_id'))
                ->select() ?: [];
            $cityStart = 11;
            //城市
            foreach ($cityList as $item1) {

                $cityAdCode = $provinceStart . $cityStart . '00';

                Db::name('area')
                    ->where('area_id', Arr::get($item1, 'area_id'))
                    ->update(['area_code' => $cityAdCode, 'area_deep' => 2]);


                $countyList = Db::name('area')
                    ->where('area_parent_id', Arr::get($item1, 'area_id'))
                    ->select() ?: [];
                $countyStart = 11;
                //区县
                foreach ($countyList as $item2) {
                    $countyAdCode = $provinceStart . $cityStart . $countyStart;

                    Db::name('area')
                        ->where('area_id', Arr::get($item2, 'area_id'))
                        ->update(['area_code' => $countyAdCode, 'area_deep' => 3]);

                    $countyStart += 1;

                }

                $cityStart += 1;

            }


            $provinceStart += 2;
        }

        sleep(5);

        //$this->__createVantArea();

        halt('生成文件完成');


    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function __createVantArea()
    {


        $privanceStr = '<?php' . PHP_EOL;
        $privanceStr .= 'return [' . PHP_EOL;

        $privanceList = Db::name('area')
            ->where('area_deep', 1)
            ->select() ?: [];

        $privanceStr .= "'province_list' => [" . PHP_EOL;
        foreach ($privanceList as $item) {


            if ($item['area_code']) {
                $privanceStr .= $item['area_code'] . ' => ' . "'" . $item['area_name'] . "'," . PHP_EOL;
            }

        }
        $privanceStr .= '],' . PHP_EOL;


        $privanceStr .= "'city_list' => [" . PHP_EOL;
        $cityList = Db::name('area')
            ->where('area_deep', 2)
            ->select() ?: [];

        foreach ($cityList as $item) {

            if ($item['area_code']) {
                $privanceStr .= $item['area_code'] . ' => ' . "'" . $item['area_name'] . "'," . PHP_EOL;
            }
        }
        $privanceStr .= '],' . PHP_EOL;


        $privanceStr .= "'county_list' => [" . PHP_EOL;
        $cityList = Db::name('area')
            ->where('area_deep', 3)
            ->select() ?: [];

        foreach ($cityList as $item) {
            if ($item['area_code']) {

                $privanceStr .= $item['area_code'] . ' => ' . "'" . $item['area_name'] . "'," . PHP_EOL;
            }
        }
        $privanceStr .= ']' . PHP_EOL;

        $privanceStr .= '];';


        file_put_contents(ROOT_PATH . 'application/extra/vantArea.php', $privanceStr);


        chmod(ROOT_PATH . 'application/extra/vantArea.php', 0777);


    }



}
